//我的求购列表
var basePage = require('../../../common/BasePage');
Page({
    behaviors: [basePage],
    data: {
        /**当前选中的订单信息 */
        order: {},
        /**tab数据源 */
        dataArr: [{
                serach: '',
                /**1:求购中,2:已锁定,3:已结束 */
                type: 1,
                title: '求购中',
                /**数据列表 */
                data: [],
                /**当前页面 */
                current_page: 0,
                /**总页数 */
                pages: 1,
                /**是否已经请求完*/
                finished: false,
            },
            {
                serach: '',
                type: 2,
                title: '已锁定',
                finished: false,
                pages: 1,
                current_page: 0,
            },
            {
                serach: '',
                type: 3,
                title: '已结束',
                finished: false,
                pages: 1,
                current_page: 0,
            }
        ],
        serachValue: '',
        serachValues: ['', '', ''], //搜索框内容缓存数组
        active: 0,
        qiugou_list_null: false, //当前求购列表为空显示空状态
        DialogShow: false
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // this.request({
        //     url: this.data.host + "/home/buy/my/items?type=" + this.data.dataArr[this.data.active].type
        // }, res => {
        //     console.log("/home/buy/my/items", res);
        // });
        this.data.dataArr[this.data.active].current_page = 0; //初始化当前页码
        this.req_items(this.data.active);

    },
    onChange(event) {
        console.log("onChange", event.detail)
        this.data.active = event.detail.index;
        this.req_items(event.detail.index)

    },
    login_reload() {
        this.req_items(  this.data.active )
      },
    /**
     * 请求我的求购
     * @param {int} index tab列表索引 0:求购中,1:已锁定,2:已结束 
     */
    req_items(index) {
        let that = this;
        let data = this.data.dataArr[index];
        let btnArr = [
            [],
            /**求购中按钮 */
            [
                /**未支付求购 */
                [{
                    text: '支付订单',
                    type: 'primary',
                    operate: 'pay'
                }, {
                    text: '取消求购',
                    type: 'default',
                    operate: 'cancel'
                }],
                /**已支付求购 */
                [{
                    text: '查看供货',
                    type: 'primary',
                    operate: 'look'
                }, {
                    text: '取消求购',
                    type: 'default',
                    operate: 'cancel'
                }]
            ],
            /**已锁定 */
            [
                /**未支付 */
                [ {
                    text: '支付订单',
                    type: 'primary',
                    operate: 'pay',
                }],
                /**已支付 */
                [{
                    text: '查看订单',
                    type: 'primary',
                    operate: 'detail',
                }]
            ],
            [{
                text: '查看订单',
                type: 'primary'
            }]
        ];
        this.request({
            url: this.data.host + "/home/buy/my/items?limit=10&type=" + data.type + "&page=" + (data.current_page + 1)
        }, res => {
            console.log("/home/buy/my/items", index, res);
            data.current_page = res.current_page;
            data.pages = res.pages;
            res.data.map(function (val) {
                val.thumb = that.get_thumb(val.order.product_img);
                if (data.type == 3) {
                    val.btnArr = btnArr[data.type]
                } else {
                    val.btnArr = btnArr[data.type][val.pay_status]
                }

            })
            data.current_page > 1 ? data.data = data.data.concat(res.data) : data.data = res.data;

            this.setData({
                dataArr: this.data.dataArr
            })
        });
    },
    /**
     * 求购单个点击事件，取消，查看供货，支付等
     */
    item_click(e) {
        console.log("item_click", e);
        let that = this;
        let order = e.detail;
        order.orderIndex = e.currentTarget.dataset.index
        if (order.btn.operate == 'cancel') {
            this.data.order = order;
            this.cancelQiugou();
        }  else if (e.detail.btn.operate == 'look'){
            wx.navigateTo({
              url: '../look_provide/provide?order_id='+order.order_id,
            })
        }else if (e.detail.btn.operate == 'pay') {
            this.request({
                url: this.data.host + '/home/payment/order',
                method: "POST",
                data: {
                    order_id: order.order_id,
                    code: 'wxpay'
                }
            }, res => {
                // console.log("res",res);
                wx.requestPayment({
                    ...res.data.pay_data,
                    success(res) {
                        //   console.log('pay success', res);
                        let order1 = that.data.dataArr[that.data.active].data[order.orderIndex];
                        //   console.log("order1",order1)
                        order1.pay_status == 1;
                        order1.btnArr = [{
                            text: '查看供货',
                            type: 'primary',
                            operate: 'look'
                        }, {
                            text: '取消求购',
                            type: 'default',
                            operate: 'cancel'
                        }];
                        that.setData({
                            dataArr: that.data.dataArr
                        })

                    },
                    fail(err) {
                        wx.showToast({
                            title: '支付失败',
                        })
                        console.error('pay fail', err)
                    }
                })
            });
        }

    },
    searchChange() {
        this.setData({
            ['serachValues[' + this.data.active + ']']: this.data.serachValue
        })
    },
    searchClear() {
        this.setData({
            ['serachValues[' + this.data.active + ']']: ''
        })
    },
    /**
     * 显示取消按钮
     */
    cancelQiugou() {
        // console.log("cancelQiugou")
        this.setData({
            DialogShow: true
        })
    },
    /**
     *确认取消求购
     */
    confirm() {
        let that = this;
        this.request({
            url: this.data.host + '/home/order/cancel',
            method: "POST",
            data: {
                order_id: this.data.order.order_id,
            }
        }, res => {
            // console.log("/home/order/cancel", res);
            let dataArr = this.data.dataArr[this.data.active].data;
            // console.log("this.data.order.orderIndex", this.data.order.orderIndex);
            // console.log("dataArr", dataArr);
            dataArr.splice(this.data.order.orderIndex, 1);
            that.setData({
                dataArr: this.data.dataArr
            })
        });
        // console.log('点击确定');
    },
    onClose() {
        this.setData({
            DialogShow: false
        });
    }
})